﻿using Ardalis.GuardClauses;
using DDD.Core.DependencyInjection;
using DDD.Core.Vaildate;
using DDD.Domain.Abstract.Entities;
using DDD.Domain.Abstract.Entities.IAbiltly;
using DDD.Domain.Abstract.GuidGenerator;

namespace DDD.Domain.Abstract.BoxMessages
{
    /// <summary>
    /// 消息箱
    /// 消息箱 当程序异常的时候的时候 消息箱主要用来存储这些消息 定时任务检查到存储箱有数据 则尝试重新发送
    /// </summary>
    public sealed class BoxMessage : Entity<Guid>, ICreateTime
    {
        public BoxMessage()
        { }

        public BoxMessage(string name, string content)
        {
            Id = IOC.KeyResolve<IGuidGenerator>(nameof(SequentialAtEndGuidGenerator)).Create();
            Name = Guard.Against.ValueIsNotNullOrEmpty(name);
            content = Guard.Against.ValueIsNotNullOrEmpty(content);
            RetryTime = 0;
            CreateTime = DateTime.Now;
        }

        /// <summary>
        /// 消息类型
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 消息内容 Json
        /// </summary>
        public string Content { get; private set; }

        /// <summary>
        /// 消息创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 重试次数
        /// </summary>
        public int RetryTime { get; private set; }

        /// <summary>
        /// 上次发送时间
        /// </summary>
        public DateTime? LastSendTime { get; private set; }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="name"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public BoxMessage Create(string name, string content)
        {
            return new BoxMessage(name, content);
        }
    }
}
